//
//  NGSUtils.m
//  demo_3
//
//  Created by 才 陈 on 12-3-9.
//  Copyright (c) 2012年 sjtu. All rights reserved.
//

#import "NGSUtils.h"

@implementation NGSUtils

static NSDictionary *properties;

+(NSString *) comment:(int)score
{
    if(score<=LEVEL_B_MINUS)
    {
        return @"Sorry, you are failed...";
    }
    else if(score<=LEVEL_B)
    {
        return @"You hit the level B";
    }
    else if(score<=LEVEL_B_PLUS)
    {
        return @"Come on, you can do it.";
    }
    else if(score<=LEVEL_A_MINUS)
    {
        return @"You just did it.";
    }
    else if(score<=LEVEL_A)
    {
        return @"Great job.";
    }
    else if(score<=LEVEL_A_PLUS)
    {
        return @"What a genius.";
    }
    else if(score<=LEVEL_S)
    {
        return @"Can't express in word.";
    }
    else
    {
        return @"Are you creazy?";
    }
    
}


+(CGSize) calculateHeightOfText:(NSString*)text Font:(UIFont*)font Width: (float)width BreakMode:(UILineBreakMode)lineBreakMode
{
    [text retain];
    [font retain];
    CGSize suggestedSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(width, FLT_MAX) lineBreakMode:lineBreakMode];
    
    [text release];
    [font release];
    
    return suggestedSize;
}


+(void) printPoint:(CGPoint)p startWith:(NSString *)startInfo
{
    NSLog(@"----------------------%@---------------------", startInfo);
    NSLog(@"[x=%f,y=%f]\n", p.x, p.y);
    NSLog(@"---------------------------------------------");
}

+(void) printRect:(CGRect)r startWith:(NSString *)startInfo
{
    NSLog(@"----------------------%@---------------------", startInfo);
    CGSize cs = r.size;
    CGPoint cp = r.origin;
    NSLog(@"[x=%f,y=%f,width=%f,height=%f]\n", cp.x, cp.y, cs.width, cs.height);
    NSLog(@"---------------------------------------------");
}

+(NSDictionary *) loadProperties:(NSString *)path :(BOOL) reload
{
    if(reload || properties==Nil)
    {
        properties = [[NSMutableDictionary alloc] init];
        NSString *content = [[[NSString alloc] initWithContentsOfFile:path
                                                         encoding:NSUTF8StringEncoding
                                                            error:nil] autorelease];
        //NSLog(@"%@", content);
        NSArray *pairs = [content componentsSeparatedByString:@"\n"];
        for (NSString *pairStr in pairs) {
            if(![NGSUtils isString:pairStr Contains:@"="]) continue;
            
            NSArray *kvStrs = [pairStr componentsSeparatedByString:@"="];
            //NSLog(@"%@=%@", [kvStrs objectAtIndex:0], [kvStrs objectAtIndex:1]);
            [properties setValue:[kvStrs objectAtIndex:1] forKey:[kvStrs objectAtIndex:0]];
        }
    }
    return properties;
}

+(BOOL) isString:(NSString *)str1 Contains:(NSString *)str2
{
    NSRange aRange = [str1 rangeOfString:str2];
    if (aRange.location ==NSNotFound) {
        return NO;
    } else {
        return YES;
    }
}
@end
